package com.cskaoyan.market.controller.Admin;

import com.cskaoyan.market.config.YmlConfig;
import com.cskaoyan.market.db.domain.MarketFeedback;
import com.cskaoyan.market.service.admin.MarketFeedBackService;

import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: YaLiFeng
 * @description: TODO
 * @date: 2024/5/8 14:20
 */
@RestController
@RequestMapping("/admin/feedback")

public class AdminFeedBackController extends HttpServlet {
    @Autowired
    private MarketFeedBackService marketFeedBackService ;
    @Autowired
    private YmlConfig ymlConfig;

    @GetMapping("list")
    public Object list(String page,String limit,String username,String sort,String order,String id,HttpServletRequest request) throws IOException {
        Integer pageParam = null;
        Integer limitParam = null;

        try {
            pageParam = Integer.parseInt(page);
            limitParam = Integer.parseInt(limit);
        } catch (NumberFormatException e) {
            return  ResponseUtil.badArgument();

        }
        ServletContext servletContext=request.getServletContext();
        List<MarketFeedback> feedbackList = marketFeedBackService.feedBackList(pageParam, limitParam, username, sort, order, id);
        String relativePath = (String) servletContext.getAttribute("path");
        for (MarketFeedback feedback : feedbackList) {
            List<String> picUrls = new ArrayList<>();
            if (feedback.getHasPicture()) {
                for (int i = 0; i < feedback.getPicUrls().length; i++) {
                    String url = feedback.getPicUrls()[i];
                    //String domain = (String) servletContext.getAttribute("domain1");
                    String address=ymlConfig.getAddress();
                    String port=ymlConfig.getPort();
                    String picUrl = "http://"+address+":"+port+"/" + url;
                    picUrls.add(picUrl);
                }
                feedback.setPicUrls(picUrls.toArray(new String[]{}));

            }
        }


        return ResponseUtil.okList(feedbackList);


    }


}
